package cn.edu.java.stream.functionalinterface;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class PredicateDemo {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        // 创建匿名类
        Predicate<Integer> evenNumberPredicate = new Predicate<Integer>() {
            @Override
            public boolean test(Integer num) {
                return num % 2 == 0;
            }
        };

        List<Integer> evenNumbers = numbers.stream().filter(evenNumberPredicate).collect(Collectors.toList());
        System.out.println("evenNumbers\t: " + evenNumbers);


        // 使用Lambda表达式
        Predicate<Integer> oddNumberPredicate = num -> num % 2 == 1;
        List<Integer> oddNumbers = numbers.stream().filter(oddNumberPredicate).collect(Collectors.toList());
        System.out.println("oddNumbers\t: " + oddNumbers);
    }
}
